//
//  SQHomeWorkTableViewCell.m
//  energy
//
//  Created by 周曼迪 on 2021/7/15.
//

#import "SQHomeWorkTableViewCell.h"

#import "SQHomeWorkModel.h"
#import "SQSpecialodel.h"

#import "NSString+NCDate.h"

@implementation SQHomeWorkTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+(CGFloat)cellHeight:(BaseModel *)model{
    return W(170);
}


-(void)setModel:(BaseModel<BaseCellModelProtocol> *)model{
    if ([model isKindOfClass:SQHomeWorkModelData.class]) {
        self.lab_one.text = @"场所区域";
        self.lab_two.text = @"工作任务";
        
        SQHomeWorkModelData * dataModel = model;
        self.lab_sa_site_area.text = dataModel.sa_site_area;
        self.lab_sa_start.text = [NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]];
        self.lab_sa_end.text = [NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]];
        self.lab_sa_work_assignment.text = dataModel.sa_work_assignment;
        
        if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 1  || [self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 0) { //未过期 和 正好
            
            if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == -1  || [self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 0) {//已过期 和 正好
             if ([dataModel.sa_inspect isEqualToString:@"0"]) {
                 self.lab_type.text = @"未完成";
                 self.lab_type.textColor = [UIColor colorNamed:@"ff00Color"];
                 self.btn_details.hidden = NO;
                 
             }else if ([dataModel.sa_inspect isEqualToString:@"1"]){//完成
                 self.lab_type.text = @"已完成";
                 self.lab_type.textColor = [UIColor colorNamed:@"19c8a8Color"];
                 self.btn_details.hidden = YES;
             }
             
             
         }else if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 1){
             self.lab_type.text = @"未开始";
             self.lab_type.textColor = [UIColor colorNamed:@"ff00Color"];
             self.btn_details.hidden = YES;
         }
            
            
            
            
            
        }else if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == -1){
            if ([dataModel.sa_inspect isEqualToString:@"1"]){//完成
                self.lab_type.text = @"已完成";
                self.lab_type.textColor = [UIColor colorNamed:@"19c8a8Color"];
                self.btn_details.hidden = YES;
            }else{
                self.lab_type.text = @"已过期";
                self.lab_type.textColor = [UIColor colorNamed:@"999Color"];
                self.btn_details.hidden = YES;
            }
          
        }
    }
  
}


- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //在指定时间前面 过了指定时间 过期
        NSLog(@"oneDay  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //没过指定时间 没过期
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //刚好时间一样.
    //NSLog(@"Both dates are the same");
    return 0;
    
}


- (IBAction)clickWorkDetails:(UIButton *)sender {
    self.cellEvent((int)self.indexPath.row);
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
